package com.example.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.Classes;
import com.example.demo.entity.User;
import com.example.demo.mapper.ClassMapper;
import com.example.demo.mapper.UserMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/classes")
public class ClassesController {

    @Resource
    ClassMapper classesMapper;
    @Resource
    UserMapper userMapper;

    @GetMapping("/user")
    public Result<?> user() {
        List<User> userList = userMapper.selectList(null);
        System.out.println(userList);
        return Result.success(userList);
    }


    @PostMapping
    public Result<?> save(@RequestBody Classes classes) {
        classesMapper.insert(classes);
        return Result.success();

    }

    @PutMapping
    public Result<?> update(@RequestBody Classes classes) {
        classesMapper.updateById(classes);
        return Result.success();

    }

    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id) {
        classesMapper.deleteById(id);
        return Result.success();

    }

    @GetMapping("/Query")
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search) {

        System.out.println("sss");

        LambdaQueryWrapper<Classes> wrapper = Wrappers.<Classes>lambdaQuery();
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(Classes::getClassName, search);
        }
        Page<Classes> stuPage = classesMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(stuPage);

    }
}